<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : AdminUserLogin.php
 * Created on : 2021-1-29 17:08:01
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace app\admin\model;

use think\Model;

/**
 * 登录失败日志
 * Class AdminUserLogin
 * Tiger <1192851302@qq.com> 2021-1-29 17:08:01
 */
class AdminUserLogin extends Model {

    /**
     * 用户登录检查
     * @param type $username
     * @return type
     */
    public static function user_logincheck($username) {
        $clientip = request()->ip();
        $time = time();
        $count = 0;
        $login = self::where(['ip' => $clientip, 'username' => $username])->find();
        $return = (empty($login) || ($time - $login['lastupdate'] > 900)) ? 5 : max(0, bcsub(5, $login['count']));
        if (empty($login)) {
            self::create([
                'ip' => $clientip,
                'username' => $username,
                'count' => $count,
                'lastupdate' => $time
            ]);
        } elseif ($time - $login['lastupdate'] > 900) {
            $count = 0;
        } else {
            $count = bcadd($login['count'], 1);
        }
        self::update(['count' => $count, 'lastupdate' => $time], ['ip' => $clientip, 'username' => $username]);
        return $return;
    }

}
